#include "SDL_ttf.h"
#include "globalVariables.h"
#include "Button.h"

#include <stdio.h>

Button::Button(wstring buttonName, int x, int y, int width, int height, SDL_Texture* buttonTexture, SDL_Texture* buttonPressedTexture)
{
	this->buttonName = buttonName;
	this->x = x;
	this->y = y;
	this->width = width;
	this->height = height;
	this->buttonTexture = buttonTexture;
	this->buttonPressedTexture = buttonPressedTexture;
	this->isPressed = false;
}

void Button::paint(SDL_Renderer* renderer)
{
	SDL_Rect rect = {x, y, this->width, this->height};
	if (this->isPressed)
	{
		SDL_RenderCopy(renderer, this->buttonPressedTexture, NULL, &rect);
	}
	else
	{
		SDL_RenderCopy(renderer, this->buttonTexture, NULL, &rect);
	}
	if (!this->buttonName.empty())
	{
		SDL_Surface* surface = TTF_RenderUNICODE_Blended(buttonFont, (Uint16*)(this->buttonName.c_str()), colorBlack);
		SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
		int texW = 0;
		int texH = 0;
		SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);
		SDL_Rect dstrect = { x+(this->width-texW)/2, y + (this->height - texH) / 2, texW, texH };
		SDL_RenderCopy(renderer, texture, NULL, &dstrect);
		//release
		SDL_FreeSurface(surface);
		SDL_DestroyTexture(texture);
	}
}

int Button::bind(PtrFunc ptrfunc)
{
	this->function = ptrfunc;
	return 0;
}
